using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Rapier.Kodachi.Shared;

namespace Rapier.Kodachi.Weather
{
    public partial class Settings : Form
    {
        private bool verified = false;
        private Host host;

        public Settings(Host host)
        {
            InitializeComponent();

            txtLocation.KeyDown += new KeyEventHandler(txtLocation_KeyDown);

            SetupBinding();
            this.host = host;
        }

        void txtLocation_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                VerifyLocationText();
            }
        }

        private void SetupBinding()
        {
            txtDays.DataBindings.Add("Text", WeatherPlugin.WeatherSettings, "ExtendedForecastDays", true, DataSourceUpdateMode.OnPropertyChanged);
            lblCurrentLocationText.DataBindings.Add("Text", WeatherPlugin.WeatherSettings, "DefaultLocationText", true, DataSourceUpdateMode.OnPropertyChanged);
            lblCurrentLocationCode.DataBindings.Add("Text", WeatherPlugin.WeatherSettings, "DefaultLocationID", true, DataSourceUpdateMode.OnPropertyChanged);
            cbCelsius.DataBindings.Add("Checked", WeatherPlugin.WeatherSettings, "UseCelsius", false, DataSourceUpdateMode.OnPropertyChanged);
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool LocationIsValid()
        {
            return VerifyLocationText();
        }

        private void bVerifyLocation_Click(object sender, EventArgs e)
        {
            VerifyLocationText();
        }

        private bool VerifyLocationText()
        {
            string requestAddress = "http://xoap.weather.com/search/search?where=" + txtLocation.Text.Trim();
            verified = false;

            try
            {
                HttpWebRequest request = WebRequest.Create(requestAddress) as HttpWebRequest;

                // Get response  
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // Load data into a dataset  
                    DataSet weather = new DataSet();
                    weather.ReadXml(response.GetResponseStream());
                    if (weather.Tables.Contains("loc") &&
                        weather.Tables["loc"].Rows.Count == 1)
                    {
                        txtLocation.Text = string.Empty;
                        verified = true;
                        lblCurrentLocationCode.Text = weather.Tables["loc"].Rows[0]["id"].ToString();
                        lblCurrentLocationText.Text = weather.Tables["loc"].Rows[0]["loc_Text"].ToString();
                    }
                    else if (!weather.Tables.Contains("loc") ||
                        (weather.Tables.Contains("loc") && 
                        weather.Tables["loc"].Rows.Count == 0))
                    {
                        verified = false;
                        //MessageBox.Show("No results found for search...");
                        NotificationPackage package = new NotificationPackage();
                        package.Message = "No results found for search...";
                        package.Caption = "No results...";
                        host.Notify(package);
                    }
                    else
                    {
                        LocationSelection select = new LocationSelection(host, weather);
                        select.ShowDialog();

                        if (select.SelectedRow != null)
                        {
                            verified = true;
                            txtLocation.Text = string.Empty;
                            lblCurrentLocationCode.Text = select.SelectedRow["id"].ToString();
                            lblCurrentLocationText.Text = select.SelectedRow["loc_Text"].ToString();
                        }
                        else
                        {
                            verified = false;
                        }
                    }
                }
            }
            catch
            {
                verified = false;
            }

            return verified;
        }

        private void txtDays_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (Convert.ToInt32(txtDays.Text) > 10 || Convert.ToInt32(txtDays.Text) < 2)
                {
                    //MessageBox.Show("Number of days must be between 2 and 10");
                    NotificationPackage package = new NotificationPackage();
                    package.Message = "Number of days must be between 2 and 10...";
                    package.Caption = "Invalid selection...";
                    host.Notify(package);
                    txtDays.Text = "3";
                }
            }
            catch
            {
                //MessageBox.Show("Number of days must be between 2 and 10");
                NotificationPackage package = new NotificationPackage();
                package.Message = "Number of days must be between 2 and 10...";
                package.Caption = "Invalid selection...";
                host.Notify(package);
                txtDays.Text = "3";
            }
        }
    }
}